//
//  BlockAlertView.m
//  BlockAlertView
//
//  Created by cocoajin on 14-1-13.
//  Copyright (c) 2014年 www.zhgu.net. All rights reserved.
//

#import "BlockAlertView.h"

@interface BlockAlertView ()

@property (nonatomic,copy)KKAlertBlock kkclickBlock;

@end

@implementation BlockAlertView

@synthesize kkclickBlock;

+ (instancetype)shareAlert
{
    static BlockAlertView *shareAlert;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareAlert = [[BlockAlertView alloc]init];
        
    });
    
    return shareAlert;
}


+ (void)showAlert:(NSString *)titile message:(NSString *)message cancel:(NSString *)cancel comfirm:(NSString *)confirm clickBlock:(KKAlertBlock)clickBlock
{
    [[BlockAlertView shareAlert] showAlert:titile message:message cancel:cancel comfirm:confirm clickBlock:clickBlock];
}



+ (void)showAlert:(NSString *)titile message:(NSString *)message cancel:(NSString *)cancel  otherBtn:(NSArray *)btnArray clickBlock:(KKAlertBlock)clickBlock
{
    [[BlockAlertView shareAlert] showAlert:titile message:message cancel:cancel otherBtn:btnArray clickBlock:clickBlock];
}


- (void)showAlert:(NSString *)titile message:(NSString *)message cancel:(NSString *)cancel comfirm:(NSString *)confirm clickBlock:(KKAlertBlock)clickBlock
{
    self.kkclickBlock = clickBlock;
    Block_release(clickBlock);
    clickBlock = nil;
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:titile message:message delegate:self cancelButtonTitle:cancel otherButtonTitles:confirm, nil];
    
    [alertView show];
    
    [alertView autorelease];
}

- (void)showAlert:(NSString *)titile message:(NSString *)message cancel:(NSString *)cancel  otherBtn:(NSArray *)btnArray clickBlock:(KKAlertBlock)clickBlock
{
    self.kkclickBlock = clickBlock;
    Block_release(clickBlock);
    clickBlock = nil;
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:titile message:message delegate:self cancelButtonTitle:cancel otherButtonTitles:nil, nil];
    for (NSString *btnTitle in btnArray) {
        [alertView addButtonWithTitle:btnTitle];
    }
    [alertView show];
    
    [alertView autorelease];
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (kkclickBlock) {
        kkclickBlock(alertView,buttonIndex);
    }
}

- (void)dealloc
{
    [[BlockAlertView shareAlert] release];
    kkclickBlock = nil;
    [super dealloc];
}

@end
